/**
 *
 */
package com.xth8013.as.controller;

import com.xth8013.as.domain.DataDictionary;
import com.xth8013.as.domain.Role;
import com.xth8013.as.service.DataDictionaryService;
import com.xth8013.as.util.DictQueryHelper;
import com.xth8013.as.util.Message;
import com.xth8013.as.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description:
 * @author: tianhua.xie
 * @Date: 2017年4月4日 下午3:41:06
 */
@Controller
@RequestMapping("/dataDictionaryMgrController")
public class DataDictionaryMgrController extends BaseController {

    @Autowired

    private DataDictionaryService dataDictionaryService;

    @RequestMapping("dataDictionaryMgr")
    public String toDataDictionaryMgr() {

        return "data_dictionary/dict_list";
    }

    @RequestMapping("preAdd")
    public String preAdd() {

        return "data_dictionary/dict_add";
    }

    @RequestMapping(value = "/preModify/{id}")
    public String preModify(@PathVariable(value = "id") Integer id, ModelMap modelMap) {
        DataDictionary dataDictionary = dataDictionaryService.getDataItemById(id);
        modelMap.put("dict", dataDictionary);
        return "data_dictionary/dict_edit";
    }

    @RequestMapping("modifyDict")
    @ResponseBody
    public Message modifyDict(DataDictionary dict) {
        Message message = null;

        try {
            this.dataDictionaryService.addDataItem(dict);
            message = coverMessage("200", "修改成功");
        } catch (Exception e) {
            message = coverMessage("500", e.getMessage());
        }

        return message;
    }


    @RequestMapping("listDict")
    @ResponseBody
    public Map<String,Object> listDict(DictQueryHelper helper , HttpServletRequest request){

        if (ObjectUtils.isBlank(helper)){
            helper = new DictQueryHelper();
        }
        return dataDictionaryService.getDictListByHelper(helper);
    }



    @RequestMapping("addDict")
    @ResponseBody
    public Message addDict(DataDictionary dict){
        Message message = null;

        try {
            this.dataDictionaryService.addDataItem(dict);
            message = coverMessage("200", "保存成功");
        } catch (Exception e) {
            message = coverMessage("500", e.getMessage());
        }

        return message;

    }

    @RequestMapping("delDict")
    @ResponseBody
    public Message delDict(String decDictIds){
        Message message = null;

        try {
            this.dataDictionaryService.deleteDataItemById(decDictIds);
            message = coverMessage("200", "删除成功");
        } catch (Exception e) {
            message = coverMessage("500", e.getMessage());
        }

        return message;

    }



}
